package com.sunni.repository.impl;

import com.sunni.entity.Reader;
import com.sunni.repository.ReaderRepository;
import com.sunni.utils.JDBCTool;

import java.sql.*;

public class ReaderRepositoryImpl implements ReaderRepository {
    Connection connection = null;
    PreparedStatement preparedStatement =null;
    ResultSet resultSet = null;


//    public static void main(String[] args) {
//        ReaderRepository readerRepository = new ReaderRepositoryImpl();
//        Reader reader01 = readerRepository.login("wuwukai", "123");
//        System.out.println("ReaderRepository---main: "+reader01);
//    }

    @Override
    public Reader login(String username, String password) {
        Reader reader =null;
        try {
            connection = JDBCTool.getConnection();
            String sql = "select * from reader where username=? and password=?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            resultSet = preparedStatement.executeQuery();
            if (resultSet.next()){
                reader = new Reader(resultSet.getInt(1),resultSet.getString(2),resultSet.getString(3),null,null,null,null);
            }
        } catch (SQLException e) {
            System.out.println(e);
        }finally {
            JDBCTool.close(connection,preparedStatement,resultSet);
        }
        return reader;
    }
}
